<!--
* description: 预约时间段选择器
* author: xlc
* createdDate: 2021-06-05
-->
<template>
  <el-col
    v-if="curIsShow"
    id="datePicker"
    :key="fullComponentKey"
    :span="span"
  >
    <el-date-picker
      v-model="modelCode"
      style="width: 100%"
      :type="dateType"
      :picker-options="pickerOptions"
      value-format="yyyy-MM-dd HH:mm:ss"
      :clearable="clearable"
      :disabled="disabled"
      :editable="false"
      range-separator="至"
      :unlink-panels="true"
      :placeholder="$t('sys.tips.esTip16')"
      start-placeholder="开始日期"
      end-placeholder="结束日期"
      @change="sendCode"
      @clear="clearCode"
    />
    <validateFormate
      :cur-label-name="curLabelName"
      :validrule="validrule"
    />
  </el-col>
</template>

<script>
import { inputMixins } from '@/components/mixins/inputMixins'
export default {
  name: 'DatePicker',
  components: {
    validateFormate: () => import('@/components/validateFormate') // 校验提示样式组件
  },
  mixins: [inputMixins],
  props: {
    dateType: { type: String, default: 'date' },
    // 日期控件选项类型
    dateOptionsType: { type: String, default: '' },
    startTime: { type: String, default: '' },
    isMul: { type: Boolean, default: true }
  },
  data() {
    return {
      modelCode: this.code !== '' && this.code ? this.code.split(',') : [],
      pickerOptions: {}
    }
  },
  created() {
    const that = this
    that.pickerOptions.disabledDate = function(time) {
      if (that.dateOptionsType) {
        switch (that.dateOptionsType) {
          // 禁用当前日期之后的日期（不包括当前日期）
          case '1':
            return time.getTime() > Date.now()
          // 禁用当前日期之前的日期（包括当前日期）
          case '2':
            return time.getTime() < Date.now()
          // 禁用当前日期之前的日期（不包括当前日期）
          case '3':
            return time.getTime() < Date.now() - 8.64e7
        }
      }
      return false
    }
  },
  methods: {
    getCharNum(strs, char) {
      return Array.from(strs).reduce((total, curChar) => {
        if (curChar === char) { total += 1 }
        return total
      }, 0)
    }
  }
}
</script>
